#!/bin/bash
set -uo pipefail

#==============================================================#
# File      :   pg-failover-callback
# Ctime     :   2018-12-06
# Mtime     :   2020-12-17
# Desc      :   Patroni event callback scripts
# Path      :   /pg/bin/pg-failover-callback
# Depend    :   pg-register pg-role
# Author    :   Vonng(fengruohang@outlook.com)
# Copyright (C) 2018-2022 Ruohang Feng
#==============================================================#

PROG_NAME="$(basename $0))"
PROG_DIR="$(cd $(dirname $0) && pwd)"

#==============================================================#
#                             Usage                            #
#==============================================================#
function usage() {
	cat <<-'EOF'
		NAME
			pg-failover-callback   -- Patroni event callback scripts

		SYNOPSIS
			pg-failover-callback <event> <role> <cluster>

		DESCRIPTION
			This is patroni pg event callback scripts, it will be invoked
			by patroni when certain event happens:
			Available events: on_stop|on_start|on_role_change
			Available roles:  master|replica
			<cluster> will be passed as target cluster name

		EXAMPLES
			pg-failover-callback  on_role_change    master  pg-test
			pg-failover-callback  on_start          replica pg-meta


	EOF
	exit 1
}

#==============================================================#
function usage() {
	cat <<-'EOF'
		NAME
			pg-failover-callback event role cluster
		
		SYNOPSIS
			This is patroni pg event callback scripts
			event could be on_stop|on_start|on_role_change
			role could be master|replica
			cluster would be target cluster name
	EOF
	exit 1
}
#==============================================================#

function on_role_change_handler() {
	local role=$1
	local cluster=$2
	local detect_role=$(pg-role)
	printf "\033[0;32m[$(date "+%Y-%m-%d %H:%M:%S")][${HOSTNAME}][role_change@${cluster}] new_role=${role} detect_role=${detect_role} \033[0m\n" >>/pg/log/callback.log

    # perform a checkpoint after failover
    printf "\033[0;32m[$(date "+%Y-%m-%d %H:%M:%S")][${HOSTNAME}] CHECKPOINT \033[0m\n" >>/pg/log/callback.log
	psql -Atqwc 'CHECKPOINT;CHECKPOINT;'

    # register new role to consul /pg/bin/pg-register $(/pg/bin/pg-role)
	printf "\033[0;32m[$(date "+%Y-%m-%d %H:%M:%S")][${HOSTNAME}] Register role=${role} \033[0m\n" >>/pg/log/callback.log
	/pg/bin/pg-register ${role} >> /pg/log/callback.log

    # reload to take effect
	printf "\033[0;32m[$(date "+%Y-%m-%d %H:%M:%S")][${HOSTNAME}] Reload consul \033[0m\n" >>/pg/log/callback.log
    consul reload
}

function on_stop_handler() {
	local role=$1
	local cluster=$2
	exit 0
}

function on_start_handler() {
	local role=$1
	local cluster=$2
	exit 0
}

function main() {
	local event=$1
	local role=$2
	local cluster=$3

	# unify role to primary & replica
	case ${role} in
	primary | p | master | m | leader | l)
		role="primary"
		patroni_role="primary"
		;;
	standby | s | replica | r | slave)
		role="replica"
		patroni_role="replica"
		;;
	*)
		echo "monitor.sh <cluster> <role>"
		exit 1
		;;
	esac

	# log event and call handler
	printf "\033[0;32m[$(date "+%Y-%m-%d %H:%M:%S")][${HOSTNAME}][event=${event}}] [cluster=${cluster}] [role=${role}]\033[0m\n" >>/pg/log/callback.log
	case ${event} in
	on_stop)
		on_stop_handler ${role} ${cluster}
		;;
	on_start)
		on_start_handler ${role} ${cluster}
		;;
	on_role_change)
		on_role_change_handler ${role} ${cluster}
		;;
	*)
		usage
		;;
	esac
}

main $@
